<template>
  <!-- 筛选区 -->
  <div class="sub-filter" v-if="filterData && !filterLoading">
    <div class="item">
      <div class="head">品牌：</div>
      <div class="body">
        <a
          href="javascript:;"
          v-for="item in filterData.brands"
          :key="item.id"
          @click="changeBrand(item.id)"
          :class="{ active: filterData.selectedBrand === item.id }"
          >{{ item.name }}</a
        >
      </div>
    </div>
    <div class="item" v-for="p in filterData.saleProperties" :key="p.id">
      <div class="head">
        {{ p.name }}
      </div>
      <div class="body">
        <a
          @click="changeAttr(p, attr)"
          :class="{ active: p.selectedProp === attr.id }"
          href="javascript:;"
          v-for="attr in p.properties"
          :key="attr.id"
          >{{ attr.name }}</a
        >
      </div>
    </div>
  </div>
  <div v-else class="sub-filter">
    <XtxSkeleton class="item" width="800px" height="40px" />
    <XtxSkeleton class="item" width="800px" height="40px" />
    <XtxSkeleton class="item" width="600px" height="40px" />
    <XtxSkeleton class="item" width="600px" height="40px" />
    <XtxSkeleton class="item" width="600px" height="40px" />
  </div>
</template>
<script>
import { ref } from "vue";
import { useRoute } from "vue-router";
import { watch } from "@vue/runtime-core";
import { findSubCategoryFilter } from "@/api/category";
export default {
  name: "SubFilter",
  setup(props, { emit }) {
    const route = useRoute();
    const filterData = ref(null); // 二级目录筛选数据
    const filterLoading = ref(false); // 加载状态

    watch(
      () => route.params.id,
      (newVal) => {
        if (newVal && route.path === `/category/sub/${route.params.id}`) {
          filterLoading.value = true;
          findSubCategoryFilter(route.params.id).then(({ result }) => {
            result.selectedBrand = null;
            result.brands.unshift({ id: null, name: "全部" });
            // 售卖属性
            result.saleProperties.forEach((p) => {
              p.selectedProp = null;
              p.properties.unshift({ id: null, name: "全部" });
            });
            filterData.value = result;
            console.log(filterData);
            filterLoading.value = false;
          });
        }
      },
      { immediate: true }
    );
    // 获取请求参数
    const getFilterParams = () => {
      const filterParams = {};
      const attrs = [];
      if (!filterData.value) return;
      filterParams.brandId = filterData.value.selectedBrand; //品牌id
      filterData.value.saleProperties.forEach((p) => {
        if (p.selectedProp) {
          const prop = p.properties.find((prop) => p.selectedProp === prop.id);
          attrs.push({ groupName: p.name, propertyName: prop.name });
        }
      });
      if (attrs.length) filterParams.attrs = attrs;
      return filterParams;
    };
    // 选择品牌
    const changeBrand = (brandId) => {
      if (filterData.value.selectedBrand === brandId) return;
      filterData.value.selectedBrand = brandId;
      emit("filter-change", getFilterParams());
    };
    // 选择属性
    const changeAttr = (p, attr) => {
      if (p.selectedProp === attr.id) return;
      p.selectedProp = attr.id;
      emit("filter-change", getFilterParams());
    };
    return { filterData, filterLoading, changeAttr, changeBrand };
  },
};
</script>
<style scoped lang='less'>
// 筛选区
.sub-filter {
  background: #fff;
  padding: 25px;
  .item {
    display: flex;
    line-height: 40px;
    .head {
      width: 80px;
      color: #999;
    }
    .body {
      flex: 1;
      a {
        margin-right: 36px;
        transition: all 0.3s;
        display: inline-block;
        &.active,
        &:hover {
          color: @xtxColor;
        }
      }
    }
  }
  .xtx-skeleton {
    padding: 10px 0;
  }
}
</style>